package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizMaterial;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.bean.BizUserMaterial;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.dao.BizMaterialDao;
import com.zkingsoft.dao.BizUserMaterialDao;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizUserMaterialService;

/**
 * @description service接口实现类(用户素材)
 * @author 姜友瑶
 * @date 2018-07-18 07:20
 */
@Service
public class  BizUserMaterialServiceImpl implements  BizUserMaterialService{

	
	@Autowired
	private BizUserMaterialDao bizUserMaterialDao;
	

	@Autowired
	private BizMaterialDao materialDao;


	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;

	
	
	@Override
	public int add(BizUserMaterial bizUserMaterial){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizUserMaterial.setCreateBy(loginUser.getSuName());
		bizUserMaterial.setUpdateBy(loginUser.getSuName());
		bizUserMaterial.setId(UUIDUtil.getRandomID());
		return bizUserMaterialDao.insert(bizUserMaterial);
		
	}
	
	@Override
	public int batchAdd(List<BizUserMaterial>  bizUserMaterialList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizUserMaterialList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizUserMaterialList.size() ? bizUserMaterialList.size() : end;
			List<BizUserMaterial> insertList = bizUserMaterialList.subList(begin, end);
			num += bizUserMaterialDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizUserMaterial oldBizUserMaterial
	,BizUserMaterial newBizUserMaterial){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizUserMaterial, newBizUserMaterial)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizUserMaterial, newBizUserMaterial);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizUserMaterial);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("id", oldBizUserMaterial.getId());
			bizUserMaterialDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizUserMaterial bizUserMaterial){
	
		return bizUserMaterialDao.updateByModel(bizUserMaterial);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizUserMaterialDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String id){
	
		return bizUserMaterialDao.deleteById(id);
	
	}
	
	@Override
	public int removeByModel(BizUserMaterial bizUserMaterial){
	
		return bizUserMaterialDao.deleteByModel(bizUserMaterial);
	
	}
	
	
	@Override
	public List<BizUserMaterial> findInPage(BizUserMaterial bizUserMaterial,  PaginationVO pageVo){
	
		return bizUserMaterialDao.selectInPage(bizUserMaterial , pageVo);
	
	}
	
	@Override
	public List<BizUserMaterial> findByModel(BizUserMaterial bizUserMaterial){
	
		return bizUserMaterialDao.selectByModel(bizUserMaterial);
	
	}
	
	@Override
	public int  findTotal(BizUserMaterial bizUserMaterial){
	
		return bizUserMaterialDao.selectTotalRecord(bizUserMaterial);
	
	}
	
	@Override
	public BizUserMaterial  findById(String id){
	
		return bizUserMaterialDao.selectById(id);
		
	}
	
	@Override
	public int collectMaterial(String id,String userId) {
		BizMaterial material = materialDao.selectById(id);
		
		BizUserMaterial um = new BizUserMaterial();
		um.setId(UUIDUtil.getRandomID());
		um.setCreateBy("system");
		um.setUpdateBy("system");
		um.setMaterialId(id);
		um.setUserId(userId);
		um.setImg(material.getImg());
		um.setPath(material.getPath());
		um.setElement(material.getElement());
		um.setEditionImg(material.getImg());
		int i=bizUserMaterialDao.insert(um);
		// 更新被收藏的次数
		material.setCollectionTimes(material.getCollectionTimes() + 1);
		materialDao.updateByModel(material);
		
		return i;
	}

 
   	
	
	
}